<template>
  <div class="system-user-dialog-container">
    <el-dialog
      :title="state.dialog.title"
      v-model="state.dialog.isShowDialog"
      width="769px"
    >
      <el-form
        ref="chargingDialogFormRef"
        :model="state.ruleForm"
        size="default"
        label-width="90px"
      >
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="计费名称">
              <el-input
                v-model="state.ruleForm.name"
                placeholder="请输入计费名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="计费类型">
              <el-select v-model="state.ruleForm.cal_type" placeholder="请选择计费类型">
                <el-option
                  v-for="item in cal_types"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="金额">
              <el-input
                v-model="state.ruleForm.charging"
                placeholder="请输入金额"
                type="number"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="备注">
              <el-input
                v-model="state.ruleForm.remark"
                placeholder="请输入备注"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="状态">
              <el-switch
                v-model="state.ruleForm.status"
                inline-prompt
                active-text="启"
                inactive-text="禁"
                :active-value="1"
                :inactive-value="2"
              ></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default">{{
            state.dialog.submitTxt
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, nextTick } from "vue";

import { getAPI } from "/@/utils/axios-utils";
import { ChargingApi, ComDataApi } from "/@/api-services/api";
import { ChargingOut, ComDatasOut } from "/@/api-services/models";

// 定义子组件向父组件传值/事件
const emit = defineEmits(["refresh"]);
const cal_types = [
  { id: 1, name: "上行+下行tokens" },
  { id: 2, name: "按次" },
  { id: 3, name: "上行tokens" },
  { id: 4, name: "下行tokens" },
];

const chargingDialogFormRef = ref();
const state = reactive({
  ruleForm: {} as ChargingOut,
  dialog: {
    isShowDialog: false,
    type: "",
    title: "",
    submitTxt: "",
  },
});

// 打开弹窗
const openDialog = (type: string, row: ChargingOut) => {
  if (type === "edit") {
    state.ruleForm = row;
    state.dialog.title = "修改计费规则";
    state.dialog.submitTxt = "修 改";
  } else {
    state.dialog.title = "新增计费规则";
    state.dialog.submitTxt = "新 增";
    // 清空表单，此项需加表单验证才能使用
    state.ruleForm = {} as ChargingOut;
    nextTick(() => {
      chargingDialogFormRef.value.resetFields();
    });
  }
  state.dialog.isShowDialog = true;
  state.dialog.type = type;
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = () => {
  chargingDialogFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return;

    if (state.dialog.type == "edit") {
      await getAPI(ChargingApi).chargingUpdate(state.ruleForm);
    } else {
      await getAPI(ChargingApi).chargingAdd(state.ruleForm);
    }
    closeDialog();
    emit("refresh");
  });
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
<style scoped lang="scss"></style>
